import { ModelType } from '@/common-typings';
import actions from '@/services/actions';
import { types } from '@/utils/utils';

const Model: ModelType = {
  namespace: 'agentReview',

  state: {
    first: [],
    second: [],
    detail: [],
    tableData: {
      list: [],
    },
  },

  effects: {
    *getUserManageListOprateion({ payload, fn }, { call, put }) {
      const response = yield call(actions.getUserManageListOprateion, payload);
      if (fn) fn(response.data);
      yield put({ key: 'agentReview/tableData', type: 'save', payload: response.data });
    },
    *getUserManageListFirst({ payload = {}, fn }, { call, put }) {
      const response = yield call(actions.getUserManageListFirst, payload);
      if (fn) fn(response);
      yield put({ key: 'agentReview/first', type: 'save', payload: response.data });
    },
    *getUserManageListSecond({ payload = {}, fn }, { call, put }) {
      const response = yield call(actions.getUserManageListSecond, payload);
      if (fn) fn(response);
      yield put({ key: 'agentReview/second', type: 'save', payload: response.data });
    },
    *updateUserManageApproveSecond({ payload = {}, fn }, { call, put }) {
      const response = yield call(actions.updateUserManageApproveSecond, payload);
      if (fn) fn(response);
      // yield put({ key: 'agentReview/approveSecond', type: 'save', payload: response.data });
    },
    *getUserManageDetail({ payload = {}, fn }, { call, put }) {
      const response = yield call(actions.getUserManageDetail, payload);
      if (fn) fn(response);
      yield put({ key: 'agentReview/detail', type: 'save', payload: response.data });
    },
    *getUserManageDownLoad({ payload = {}, fn }, { call, put }) {
      const response = yield call(actions.getAgentManageDownLoad, payload);
      if (fn) fn(response);
    },
  },

  reducers: {
    save(state: any, action) {
      let result = {};
      const list: any[] = [];
      // 过滤器具
      const filters = (data: any) => {
        if (types(data) === 'array') {
          data = data.filter((i: string) => i);
          data.forEach((item: string) => {
            list.push({
              label: item,
              value: item,
            });
          });
        }
      };

      switch (action.key) {
        case 'agentReview/tableData':
          result = { tableData: action.payload || {} };
          break;
        case 'agentReview/first':
          filters(action.payload);
          result = { first: list };
          break;
        case 'agentReview/second':
          filters(action.payload);
          result = { second: list };
          break;
        case 'agentReview/detail':
          result = { detail: action.payload || {} };
          break;
        default:
          break;
      }
      return {
        ...state,
        ...result,
      };
    },
  },
};

export default Model;
